if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('InvReceiveOrder') and o.name = 'FK_INVRECEI_REFERENCE_BDACCOUN_16')
alter table InvReceiveOrder
   drop constraint FK_INVRECEI_REFERENCE_BDACCOUN_16
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('InvReceiveOrder') and o.name = 'FK_INVRECEI_REFERENCE_BDACCOUN_17')
alter table InvReceiveOrder
   drop constraint FK_INVRECEI_REFERENCE_BDACCOUN_17
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('InvReceiveOrder') and o.name = 'FK_INVRECEI_REFERENCE_BDACCOUN_18')
alter table InvReceiveOrder
   drop constraint FK_INVRECEI_REFERENCE_BDACCOUN_18
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('InvReceiveOrder') and o.name = 'FK_INVRECEI_REFERENCE_BDORGUNI')
alter table InvReceiveOrder
   drop constraint FK_INVRECEI_REFERENCE_BDORGUNI
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('InvReceiveOrder') and o.name = 'FK_INVRECEI_REFERENCE_BDCATEGO_STATUS')
alter table InvReceiveOrder
   drop constraint FK_INVRECEI_REFERENCE_BDCATEGO_STATUS
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('InvReceiveOrderDetail') and o.name = 'FK_INVRECEI_REFERENCE_INVRECEI')
alter table InvReceiveOrderDetail
   drop constraint FK_INVRECEI_REFERENCE_INVRECEI
go

if exists (select 1
            from  sysobjects
           where  id = object_id('InvReceiveOrder')
            and   type = 'U')
   drop table InvReceiveOrder
go

/*==============================================================*/
/* Table: InvReceiveOrder                                       */
/*==============================================================*/
create table InvReceiveOrder (
   FId                  varchar(50)          not null,
   FNumber              varchar(200)         not null,
   FName                varchar(200)         not null,
   FBillDate            datetime             not null,
   FCreateTime          datetime             not null,
   FCreatorId           varchar(50)          null,
   FModifyTime          datetime             null,
   FModifierId          varchar(50)          null,
   FApproveTime         datetime             null,
   FApproverId          varchar(50)          null,
   FRemark              text                 null,
   FOrgunitId           varchar(50)          null,
   FStatusId            varchar(50)          null,
   constraint PK_INVRECEIVEORDER primary key (FId)
)
go

alter table InvReceiveOrder
   add constraint FK_INVRECEI_REFERENCE_BDACCOUN_16 foreign key (FApproverId)
      references BdAccount (FId)
go

alter table InvReceiveOrder
   add constraint FK_INVRECEI_REFERENCE_BDACCOUN_17 foreign key (FCreatorId)
      references BdAccount (FId)
go

alter table InvReceiveOrder
   add constraint FK_INVRECEI_REFERENCE_BDACCOUN_18 foreign key (FModifierId)
      references BdAccount (FId)
go

alter table InvReceiveOrder
   add constraint FK_INVRECEI_REFERENCE_BDORGUNI foreign key (FOrgunitId)
      references BdOrgunit (FId)
go

alter table InvReceiveOrder
   add constraint FK_INVRECEI_REFERENCE_BDCATEGO_STATUS foreign key (FStatusId)
      references BdCategory (FId)
go


alter table InvReceiveOrderDetail
   add constraint FK_INVRECEI_REFERENCE_INVRECEI foreign key (FParentId)
     references InvReceiveOrder (FId)
go